//
//  UIBarButtonItem-Extension.swift
//  DYZB
//
//  Created by HOu on 2021/11/18.
//

import UIKit

extension UIBarButtonItem {
    /*
    // 类方法
    class func createItem(imageName : String , highImageName : String , size : CGSize) -> UIBarButtonItem{
        let btn = UIButton()
        
        btn.setImage(UIImage(named: imageName), for: .normal)
        btn.setImage(UIImage(named: highImageName), for: .highlighted)
        btn.frame = CGRect(origin: CGPoint.zero, size: size)
        
        return UIBarButtonItem(customView: btn)
        
    }*/
    
    //构造函数  建议使用
    //属于便利构造函数
    // 1.convenience开头
    // 2.在构造函数中必须明确调用一个设计的构造函数(self)
    convenience init(imageName : String , highImageName : String = "" , size : CGSize = CGSize.zero){
         
        //1.创建UIButton
        let  btn = UIButton()
        btn.backgroundColor = UIColor.orange
        //2.设置btn的图片
        btn.setImage(UIImage(named: imageName), for: .normal)
        
        if highImageName != ""{
            btn.setImage(UIImage(named: highImageName), for: .highlighted)
        }
        //3.设置btn的尺寸
        if size == CGSize.zero{
            btn.sizeToFit()
        }else{
            btn.frame = CGRect(origin: CGPoint.zero, size: size)
        }
        
        //创建UIBarButtonitem
        btn.frame = CGRect(origin: CGPoint.zero, size: size)
         
         self.init(customView:btn)
    }
    
    
    
}
